//
//  JMCarKeyCell.m
//  OpenSesame
//
//  Created by zhangjiaming on 15/12/25.
//  Copyright © 2015年 OpenSesame. All rights reserved.
//

#import "JMCarKeyCell.h"
#import "JMCar.h"
@interface JMCarKeyCell()

@property (weak, nonatomic) UIImageView *iconView;
@property (weak, nonatomic) UILabel *keyNameLabel;
@property (weak, nonatomic) UILabel *fitLabel;
@property (weak, nonatomic) UILabel *frequencyLabel;
@property (weak, nonatomic) UIImageView *specification;
@end

@implementation JMCarKeyCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // 设置车钥匙头像
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
        // 设置车钥匙名称Label
        UILabel *keyNameLabel = [[UILabel alloc] init];
        keyNameLabel.textColor = JMColor(51, 51, 51);
        keyNameLabel.font = [UIFont systemFontOfSize:15];
        self.keyNameLabel = keyNameLabel;
        [self.contentView addSubview:keyNameLabel];
        
        // 设置频率label
        UILabel *frequencyLabel = [[UILabel alloc] init];
        [self.contentView addSubview:frequencyLabel];
        frequencyLabel.textColor = JMColor(153, 153, 153);
        frequencyLabel.font = [UIFont systemFontOfSize:11];
        self.frequencyLabel = frequencyLabel;
        
        // 设置右边说明书
        UIImageView *specification = [[UIImageView alloc] init];
        [self.contentView addSubview:specification];
        specification.image = [UIImage imageNamed:@"说明书"];
        self.specification = specification;
        
        // 设置适配label
        UILabel *fitLabel = [[UILabel alloc] init];
        fitLabel.textColor = JMColor(102, 102, 102);
        fitLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:fitLabel];
        self.fitLabel = fitLabel;
        
        // 设置cell之间的分割线
        UIView *separater = [[UIView alloc] initWithFrame:CGRectMake(0, 95, JMPhoneWidth, 3)];
        separater.backgroundColor = JMColor(242, 242, 242);
        [self.contentView addSubview:separater];
        
        // 设置选中时的状态颜色
        UIView *back = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 98)];
        back.backgroundColor = JMColor(245, 245, 245);
        self.selectedBackgroundView = back;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
    
    return self;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"key";
    JMCarKeyCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[JMCarKeyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

/** 设置子控件的frame和显示数据 */
- (void)setDisplayFrame:(JMDisplayFrame *)displayFrame {
    _displayFrame = displayFrame;
    // 设置数据
    [self settingData];
    
    // 设置frame
    [self settingFrame];
}

/**
 *  设置frame
 */
- (void)settingFrame {
    // 头像
    self.iconView.frame = self.displayFrame.iconF;
    
    // 钥匙名称
    self.keyNameLabel.frame = self.displayFrame.keyNameLabelF;
    
    // 频率
    self.frequencyLabel.frame = self.displayFrame.frequencyLabelF;
    
    // 说明书
    self.specification.frame = self.displayFrame.specificationF;
    
    // 适配
    self.fitLabel.frame = self.displayFrame.fitLabelF;
}

/**
 *  设置数据
 */
- (void)settingData {
    // 汽车数据
    JMCar *car = self.displayFrame.car;
    
    // 头像
    //self.iconView.image = [UIImage imageNamed:car.pic];
    self.iconView.image = [UIImage imageNamed:@"产品图"];
    
    // 钥匙名称
    self.keyNameLabel.text = car.name;
    
    // 频率
    NSString *frequencyStr = [NSString stringWithFormat:@"%@MHz", car.pinlv];
    self.frequencyLabel.text = frequencyStr;
    
    // 说明书
    self.specification.image = [UIImage imageNamed:@"说明书"];
    
    // 适配
    self.fitLabel.text = car.desurl;
}
@end
